package com.xxxt.hrs.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;


import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.xxxt.dao.StudentMapper;
import com.xxxt.entity.ClassGrade;
import com.xxxt.entity.Student;
import com.xxxt.entity.Teacher;

/**
 * @ClassName MqManageAsynController
 * @Author MoPeiwen
 * @Date 2020年3月12日 上午12:19:07
 */
@Service
@Transactional
public class ImportFormAsynController {
	@Autowired
	StudentMapper studentMapper;
	@Autowired
	SqlSessionFactory sqlSessionFactory;
	
	@Async // 异步标签
	public Future<String> imAccountForm(List<Map<String, Object>> list,String tno,List<Student> newnewList1) {
		int i = 0;
		//线程开始时间
		long time = System.currentTimeMillis();
		//打印线程ID
		System.err.println(Thread.currentThread().getId()+"线程开始,时间:"+System.currentTimeMillis());
		
		// map数据解析,写入数据库
		Teacher teacher = new Teacher(tno);
		//处理list
		
			//学生缓存池
			//List<YqStudent> sList = studentDao.queryStudents();
			for (Map<String, Object> map : list) {
				// 插入数据到学生表
				//缔造学生基本信息
				Student student = new Student();
				for (Student student2 : newnewList1) {
					if(student2.getSno().equals((String) map.get("sno"))) {
						student.setSno((String) map.get("sno"));
						///缔造学生和班级的关联
						student.setClassGrade(student2.getClassGrade());
					}	
				}
				student.setName((String) map.get("name"));
				student.setPassword("5a27dd5e0c9ed7861f9007a32a61c46d");
				//缔造与学生关联的教师
				student.setTeacher(teacher);
				//添加学生
				studentMapper.addStudent(student);
				i++;// 统计插入数量
				if (i % 500 == 0) {// 如果到500条数据 即写入数据库
					if (i!=0) {
						sqlSessionFactory.openSession(true).commit();
						System.err.println("已写入数据库第" + i + "条数据");
					}
				}
			} // for结束
		//打印线程结束时间
		System.err.println(Thread.currentThread().getId()+"线程结束,时间:"+(System.currentTimeMillis()-time)/1000+"s");
		return null;
	}
}